package com.ruoyi.life.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.domain.vo.CreateBillVo;
import com.ruoyi.life.domain.vo.QyEnterpriseOtherFeeVO;
import com.ruoyi.life.service.IFeLeaseRecordService;
import com.ruoyi.omp.api.domain.FeLeaseRecord;
import com.ruoyi.omp.api.domain.vo.RoomVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 租赁记录Controller
 *
 * @author cjie
 * @date 2021-09-17
 */
@Api(tags = " 租赁记录")
@RestController
@RequestMapping("/feLeaseRecord")
public class FeLeaseRecordController extends BaseController {
    @Autowired
    private IFeLeaseRecordService feLeaseRecordService;

    /**
     * 查询租赁记录列表
     */
    @ApiOperation(value="查询租赁记录列表", notes="查询租赁记录列表")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(FeLeaseRecord feLeaseRecord) {
        startPage();
        List<FeLeaseRecord> list = feLeaseRecordService.selectFeLeaseRecordList(feLeaseRecord);
        return getDataTable(list);
    }


    @ApiOperation(value="查询租赁记录列表", notes="查询租赁记录列表")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:list")
    @GetMapping("/otherFeeList")
    public TableDataInfo listQyEnterpriseFee(FeLeaseRecord feLeaseRecord) {
        startPage();
        List<QyEnterpriseOtherFeeVO> list = feLeaseRecordService.selectOtherFeeList(feLeaseRecord);
        return getDataTable(list);
    }

    /**
     * 获取租赁记录详细信息
     */
    @ApiOperation(value="获取租赁记录详细信息", notes="获取租赁记录详细信息")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:query")
    @GetMapping(value = "/otherFee/{id}")
    public AjaxResult getOtherFeeById(@PathVariable("id") String id) {
        return AjaxResult.success(feLeaseRecordService.selectOtherFeeById(id));
    }


    /**
     * 获取租赁记录详细信息
     */
    @ApiOperation(value="获取租赁记录详细信息", notes="获取租赁记录详细信息")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:query")
    @PostMapping(value = "/updateOtherFee")
    public AjaxResult updateOtherFee(@RequestBody QyEnterpriseOtherFeeVO vo) {
        return AjaxResult.success(feLeaseRecordService.updateOtherFee(vo));
    }






    @ApiOperation(value="修改租赁生效", notes="修改租赁生效")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:edit")
    @PostMapping("/updateEffect")
    public AjaxResult  updateEffect(@RequestBody FeLeaseRecord feLeaseRecord){
        return toAjax(feLeaseRecordService.updateFeLeaseRecord(feLeaseRecord));
    }


    @ApiOperation(value="手动生成账单", notes="手动生成账单")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:edit")
    @PostMapping("/createBill")
    public AjaxResult  createBill(@RequestBody CreateBillVo createBillVo){
        return toAjax(feLeaseRecordService.createBill(createBillVo));
    }




    @GetMapping("/getLeaseRecordByEnterprise")
    public R<FeLeaseRecord>  getLeaseRecordByEnterprise(String enterpriseId){
        return R.ok(feLeaseRecordService.selectFeLeaseRecordByEnterpriseId(enterpriseId));
    }


    @GetMapping("/getRecordListByEnterpriseId")
    public R<List<FeLeaseRecord>>  getRecordListByEnterpriseId(String enterpriseId){
        List<FeLeaseRecord> records =   feLeaseRecordService.getRecordListByEnterpriseId(enterpriseId);
        return R.ok(records);
    }

    /**
     * 查询租赁记录列表(后台调用)
     */
    @PostMapping("/listRecord")
    public R<List<FeLeaseRecord>> listRecord(FeLeaseRecord feLeaseRecord) {
        List<FeLeaseRecord> list = feLeaseRecordService.selectFeLeaseRecordList(feLeaseRecord);
        List<FeLeaseRecord> records = new ArrayList<>();
        list.forEach( o -> {
           FeLeaseRecord record = feLeaseRecordService.selectFeLeaseRecordById(o.getId());
            records.add(record);
        });
        return R.ok(records);
    }

    /**
     * 导出租赁记录列表
     */
    @ApiOperation(value="导出租赁记录列表", notes="导出租赁记录列表")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:export")
    @Log(title = "租赁记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeLeaseRecord feLeaseRecord) throws IOException {
        List<FeLeaseRecord> list = feLeaseRecordService.selectFeLeaseRecordList(feLeaseRecord);
        ExcelUtil<FeLeaseRecord> util = new ExcelUtil<FeLeaseRecord>(FeLeaseRecord.class);
        util.exportExcel(response, list, "租赁记录数据");
    }
    /**
     * 获取租赁记录详细信息
     */
    @ApiOperation(value="获取租赁记录详细信息", notes="获取租赁记录详细信息")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(feLeaseRecordService.selectFeLeaseRecordById(id));
    }

    /**
     * 通过租赁记录id查询房屋信息（后台调用)
     * @param recordId 租赁记录id
     * @return
     */
    @GetMapping(value = "/roomList/{recordId}")
    public R<List<RoomVO>> selectRoomByRecordId(@PathVariable("recordId") String recordId){
        return R.ok(feLeaseRecordService.selectRoomByRecordId(recordId));
    }

    /**
     * 新增租赁记录(后台调用）
     */
    @PostMapping("/add")
    public R addRecord(@RequestBody FeLeaseRecord feLeaseRecord) {
        return toR(feLeaseRecordService.insertFeLeaseRecord(feLeaseRecord));
    }

    /**
     * 新增租赁记录
     */
    @ApiOperation(value="新增租赁记录", notes="新增租赁记录")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:add")
    @Log(title = "租赁记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeLeaseRecord feLeaseRecord) {
        return toAjax(feLeaseRecordService.insertFeLeaseRecord(feLeaseRecord));
    }

    /**
     * 修改租赁记录(后台调用)
     */
    @PostMapping("/update")
    public R update(@RequestBody FeLeaseRecord feLeaseRecord) {
        return toR(feLeaseRecordService.updateFeLeaseRecord(feLeaseRecord));
    }


    @PostMapping("/delByEnterpriseId/{enterpriseId}")
    public R delByEnterpriseId(@PathVariable("enterpriseId") String enterpriseId){
        return toR(feLeaseRecordService.delByFeLeaseRecordByEnterpriseId(enterpriseId));


    }



    /**
     * 修改租赁记录
     */
    @ApiOperation(value="修改租赁记录", notes="修改租赁记录")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:edit")
    @Log(title = "租赁记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeLeaseRecord feLeaseRecord) {
        return toAjax(feLeaseRecordService.updateFeLeaseRecord(feLeaseRecord));
    }

    /**
     * 删除租赁记录
     */
    @ApiOperation(value="删除租赁记录", notes="删除租赁记录")
    @ApiImplicitParam(name = "ids", value = "租赁记录ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:feLeaseRecord:remove")
    @Log(title = "租赁记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(feLeaseRecordService.deleteFeLeaseRecordByIds(ids));
    }
}
